package com.intel.two.controller.record;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.intel.two.common.LayuiPageVo;
import com.intel.two.common.R;
import com.intel.two.pojo.record.InformationRecord;
import com.intel.two.pojo.record.listener.WebRecordListener;
import com.intel.two.pojo.record.recordbo.InformationRecordBo;
import com.intel.two.service.record.InformationRecordService;
import com.intel.two.utils.OssUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Map;


@RestController
@RequestMapping("/informationRecord")
public class InformationRecordController {
    @Autowired
    private InformationRecordService informationRecordService;
    @RequestMapping("/informationRecordList")
    public LayuiPageVo<InformationRecord> selectAllRecord(@RequestParam Map<String,Object> params){
        return informationRecordService.selectAllRecord(params);
    }
    @PostMapping("/informationRecordAdd")
    public R recordAdd(@RequestBody InformationRecord informationRecord)  {

        int i = informationRecordService.recordAdd(informationRecord);
        if (i!=0){
            return R.ok();

        }else {
            return R.error();
        }


    }
    @RequestMapping("/upload")
    public R uploadPic(MultipartFile file) throws Exception {
        //得到文件的名字
        String originalFilename = file.getOriginalFilename();
        //第一个参数传这个文件的流 第二个传文件的名字
        assert originalFilename != null;
        String fileUrl = OssUtil.updateLoad(file.getInputStream(),originalFilename);
        //上传成功后把文件地址写给前端
        return R.ok().data("url",fileUrl);

    }
    @GetMapping("/deleteInformationRecord")
    public R deleteInformationRecord(Integer id){
        System.out.println(id);
        int i = informationRecordService.deleteInformationRecord(id);
        return i!=0?R.ok():R.error();
    }
    @PostMapping("/deleteInformationRecordList")
    public R deleteInformationRecordList(String ids){
        int i = informationRecordService.deleteInformationRecordList(ids);
        return i!=0?R.ok():R.error();
    }
    @RequestMapping("/update")
    public R update(InformationRecord informationRecord){
        //System.out.println(informationRecord);
        int i = informationRecordService.update(informationRecord);
        return i!=0?R.ok():R.error();
    }
    @Autowired
    WebRecordListener webRecordListener;
    //读取excel文件 把excel中的数据存入数据库
    @RequestMapping("/import")
    public R importExcel(MultipartFile file) throws IOException {
        /**
         * 参数一：文件流
         * 参数二：读取到的数据封装到哪个类型
         * 参数三：监听器 每读取一行数据会执行invoke方法
         */
       ExcelReaderBuilder read= EasyExcel.read(file.getInputStream(), InformationRecordBo.class,webRecordListener);
       //得到读取的sheet页对象
        ExcelReaderSheetBuilder sheet = read.sheet();
        //读取sheet
        sheet.doRead();
        return R.ok();
    }
    @RequestMapping("/templateDownload")
    public void templateDownload(HttpServletRequest request, HttpServletResponse response) throws IOException {
       //下载 excel文件设置响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //获取模板路径
        String realPath = request.getServletContext().getRealPath("/template/居民档案模板.xlsx");

        //根据文件的真实路径 得到文件对象
        File file = new File(realPath);
        //将文件对象转为字节数组
        byte[] bytes = FileUtils.readFileToByteArray(file);

        //把模板下载到前端
        String filename = URLEncoder.encode("居民模板档案", "utf-8");//防止中文乱码
        //通过这个响应头告诉浏览器 下载的文件名字是什么
        response.setHeader("content-Disposition","attachment;filename="+filename+".xlsx");
        //得到指向浏览器的输出流
        ServletOutputStream outputStream = response.getOutputStream();

        outputStream.write(bytes);
        outputStream.close();
    }

}
